package org.lanyu.springainovel.chat.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.lanyu.springainovel.chat.service.ChatSessionHistoryService;
import org.lanyu.springainovel.chat.service.ChatMemoryService;
import org.lanyu.springainovel.common.entity.SpringAIChatMemory;
import org.lanyu.springainovel.common.entity.ChatSessionHistory;
import org.lanyu.springainovel.common.vo.RestVO;
import org.lanyu.springainovel.common.util.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "会话历史接口", description = "会话历史记录相关接口")
@RestController
@RequestMapping("/sessionHistory")
public class ChatSessionHistoryController {
    @Autowired
    private ChatSessionHistoryService historyService;

    @Autowired
    private ChatMemoryService chatMemoryService;

    @Operation(summary = "新增会话历史")
    @PostMapping(value = "/create", produces = "application/json")
    public RestVO<ChatSessionHistory> create(@RequestBody ChatSessionHistory session) {
        ChatSessionHistory s = historyService.createSession(session);
        return RestVO.success(s);
    }

    @Operation(summary = "获取当前用户所有会话历史")
    @GetMapping(value = "/list", produces = "application/json")
    public RestVO<List<ChatSessionHistory>> list() {
        // 从Security上下文中获取当前用户ID
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return RestVO.fail("用户未登录");
        }
        
        List<ChatSessionHistory> list = historyService.getSessionsByUser(userId);
        return RestVO.success(list);
    }

    @Operation(summary = "根据sessionId获取历史对话气泡")
    @GetMapping(value = "/get", produces = "application/json")
    public RestVO<List<SpringAIChatMemory>> getBySessionId(@RequestParam String sessionId) {
        List<SpringAIChatMemory> list = chatMemoryService.getSessionDetail(sessionId);
        return RestVO.success(list);
    }

    @Operation(summary = "更新会话历史")
    @PostMapping(value = "/update", produces = "application/json")
    public RestVO<ChatSessionHistory> update(@RequestBody ChatSessionHistory session) {
        ChatSessionHistory s = historyService.updateSession(session);
        return RestVO.success(s);
    }

    @Operation(summary = "清除会话历史")
    @PostMapping(value = "/clear/{sessionId}", produces = "application/json")
    public RestVO<Void> clearSession(@PathVariable String sessionId) {
        // 调用MemoryController中的清除会话方法
        chatMemoryService.clearSessionHistory(sessionId);
        return RestVO.success(null);
    }

    @Operation(summary = "获取会话详情")
    @GetMapping(value = "/{sessionId}", produces = "application/json")
    public RestVO<List<SpringAIChatMemory>> getSessionDetail(@PathVariable String sessionId) {
        List<SpringAIChatMemory> list = chatMemoryService.getSessionDetail(sessionId);
        return RestVO.success(list);
    }

    @Operation(summary = "删除会话")
    @DeleteMapping(value = "/{sessionId}", produces = "application/json")
    public RestVO<Void> deleteSession(@PathVariable String sessionId) {
        historyService.deleteSessionHistory(sessionId);
        return RestVO.success(null);
    }

    @Operation(summary = "删除用户所有会话")
    @DeleteMapping(value = "/user/all", produces = "application/json")
    public RestVO<Void> deleteAllSessionsByUser() {
        // 从Security上下文中获取当前用户ID
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return RestVO.fail("用户未登录");
        }
        
        historyService.deleteAllSessionsByUser(userId);
        return RestVO.success(null);
    }
}
